﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EpcTagLibrary
{
    public static class EPCFactory
    {
        public static EPC NewInstance(string rawData, int radix)
        {
            string Temp = rawData.Trim();
            Temp = Temp.Replace(" ", "");

            if (radix == 2)
            {
                return NewInstance(Common.RadixStringToByteArray(rawData, 2));
            }
            else if (radix == 10)
            {
                return NewInstance(Common.RadixStringToByteArray(rawData, 10));
            }
            else if (radix == 16)
            {
                return NewInstance(Common.RadixStringToByteArray(rawData,16));
            }

            throw new EPCParseException("Could not create EPC tag for data: " + rawData);
        }

        public static EPC NewInstance(string rawData)
        {
            if (rawData == null)
            {
                throw new NullReferenceException("Null Tag Data");
            }

            if (Common.IsBinaryString(rawData))
            {
                return NewInstance(rawData, 2);
            }
            else if (Common.IsDecimalString(rawData))
            {
                return NewInstance(rawData,10);
            }
            else if (Common.IsHexString(rawData))
            {
                return NewInstance(rawData, 16);
            }

            throw new EPCParseException("Could not create EPC tag for data: " + rawData);
        }

        public static EPC NewInstance(byte[] data)
        {
            EPC Epc = null;

            Bits Bs = Common.ByteArrayToBits(data);
            Bits HeaderBits = Bs.GetBits(Bs.Length - 8, Bs.Length - 1);
            Byte Header = Common.BitsToByteArray(HeaderBits)[0];

            if (Header == 47)
            {
                Epc = new DOD(data);
            }
            else if (Header == 48)
            {
                Epc = new SGTIN(data);
            }
            else if (Header == 49)
            {
                Epc = new SSCC(data);
            }
            else if (Header == 50)
            {
                Epc = new SGLN(data);
            }
            else if (Header == 51)
            {
                Epc = new GRAI(data);
            }
            else if (Header == 52)
            {
                Epc = new GIAI(data);
            }
            else if (Header == 53)
            {
                Epc = new GID(data);
            }
            else if (Header == 54)
            {
                Epc = new SGTIN(data);
            }
            else if (Header == 55)
            {
                Epc = new GRAI(data);
            }
            else if (Header == 56)
            {
                Epc = new GIAI(data);
            }
            else if (Header == 57)
            {
                Epc = new SGLN(data);
            }
            else
            {
                Epc = new RAW(data);
            }

            return Epc;
        }
    }
}
